package net.viralpatel.spring3.controller;
 
import net.viralpatel.spring3.form.Contact;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
 
@Controller
@SessionAttributes
public class ContactController {
 
    @RequestMapping(value = "/addContact", method = RequestMethod.POST)
    public String addContact(@ModelAttribute("contact")
                            Contact contact, BindingResult result) {
         
        System.out.println("First Name:" + contact.getFirstname() + 
                    "Last Name:" + contact.getLastname());
         
        return "redirect:contacts.html";
    }
     
    @RequestMapping("/contacts")
    public ModelAndView showContacts() {
         
        return new ModelAndView("contact", "command", new Contact());
    }
}	

/*

http://viralpatel.net/blogs/spring-3-mvc-handling-forms/

In above controller class, note that we have created two methods with Request Mapping
/contacts and /addContact. The method showContacts() will be called
when user request for a url contacts.html. This method will render a model with name
“contact”. Note that in the ModelAndView object we have passed a blank Contact object
 with name “command”. The spring framework expects an object with name command if you 
 are using <form:form> in your JSP file.
 
Also note that in method addContact() we have annotated this method with RequestMapping 
and passed an attribute method=”RequestMethod.POST”. Thus the method will be called only 
when user generates a POST method request to the url /addContact.html. We have annotated 
the argument Contact with annotation @ModelAttribute. This will binds the data from request 
to the object Contact. In this method we just have printed values of Firstname and Lastname 
and redirected the view to cotnacts.html.

*/